/*© 2010-2011 mOcean Mobile. A subsidiary of Mojiva, Inc. All Rights Reserved.*/
package com.adserver.adview;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.SimpleFormatter;

public class Logger {
    private static java.util.logging.Logger logger = java.util.logging.Logger.getLogger("AdServerView");

    private FileHandler fileHandler = null;
    private Level level = Level.INFO;

    private static Logger logger2;

    public Logger() throws IOException {
        setOutputFile("adserver.log");
    }

    public static Logger getInstance() {
        if (logger2 == null) {
            try {
                logger2 = new Logger();
            } catch (IOException e) {
            }
        }
        return logger2;
    }

    public void setOutputFile(String filename) throws IOException {
        if (fileHandler != null) {
            logger.removeHandler(fileHandler);
        }
        fileHandler = new FileHandler(filename);
        fileHandler.setFormatter(new SimpleFormatter());
        logger.addHandler(fileHandler);
        setLevel(level);
    }

    public void setLevel(Level level) {
        this.level = level;
        logger.setLevel(level);
    }

    public synchronized void logText(Level level, String text) {
        logger.log(level, text);
    }

    public synchronized void logText(Level level, String text, Throwable throwable) {
        logger.log(level, text);
        logger.log(level, throwable.getMessage());
    }
}
